﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Android;
using UnityEngine.UI;
using Z1029.PhiPlayer.OpenSource;

public class MenuCtrl : MonoBehaviour 
{
    public static MenuCtrl instance;
    public static bool overMenuLoaded;

    public TextAsset levelSets;
    public Dropdown chooseLevel;
    public Text diffcultyShow;
    public LevelInfo.HarderLV lv;

	public void Start ()
    {
        if (Application.platform == RuntimePlatform.Android && !Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead))
        {
            Permission.RequestUserPermissions(new string[] {
                Permission.ExternalStorageRead,
                Permission.ExternalStorageWrite });
        }
        instance = this;
        if (!overMenuLoaded)
        {
            //AssetBundle.LoadFromMemory(ab);
            //overMenuLoaded = true;
        }
        try
        {
            if(!Directory.Exists(FileManager.ResourcesPath))
            {
                Directory.CreateDirectory(FileManager.ResourcesPath);
            }
            List<Dropdown.OptionData> options = new List<Dropdown.OptionData>();
            DirectoryInfo di = new DirectoryInfo(FileManager.ResourcesPath);
            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                options.Add(new Dropdown.OptionData(dir.Name));
            }
            foreach (FileInfo fi in di.GetFiles())
            {
                if (fi.Name.Trim().EndsWith(".zip")) ;//TODO
                    //options.Add(new Dropdown.OptionData(fi.Name));
            }
            chooseLevel.ClearOptions();
            chooseLevel.AddOptions(options);
        }
        catch (Exception e)
        {
            Utils.AndroidToastStringShow(e.Message);
            //throw e;
        }
        diffcultyShow.text = lv.ToString();
        Application.targetFrameRate = 90;
        Screen.autorotateToLandscapeLeft = true;
        Screen.autorotateToLandscapeRight = true;
        Screen.autorotateToPortrait = false;
        Screen.autorotateToPortraitUpsideDown = false;
        Screen.orientation = ScreenOrientation.AutoRotation;
    }
	
	//void Update () 
	//{
		
	//}

    public void ChangeDifficulty()
    {
        lv += 1;
        if ((int)lv == 5)
            lv = 0;
        diffcultyShow.text = lv.ToString();
    }

    public void LoadLevel()
    {
        string musicPath = chooseLevel.captionText.text.ToString().Trim();
        string resPath = PlayerPrefs.GetString("ResourcesPath", Application.persistentDataPath + "/Tracks");
        string path = resPath +"/" + chooseLevel.captionText.text.ToString().Trim();
        if (path.EndsWith(".zip") && !Directory.Exists(path) && File.Exists(path))
        {
            //ZipUtil.UnZip(path, path.Replace(".zip", ""));
            //TODO
            File.Delete(path);
            path = path.Replace(".zip", "");
            musicPath = musicPath.Replace(".zip", "");
        }
        LevelController.musicPath = musicPath;
        string musicName;
        string levelInfo;
        int harder;
        if (musicPath.Contains("."))
        {
            musicName = musicPath.Split('.')[0];
            levelInfo = musicPath.Split('.')[1] + "," + musicPath.Split('.')[2];
            if(musicPath.Split('.').Length >= 4)
            {
                try
                {
                    harder = int.Parse(musicPath.Split('.')[3 + (int)lv]);
                 }
                catch
                {
                    try
                    {
                        harder = int.Parse(musicPath.Split('.')[3]);
                    }
                    catch
                    {
                        harder = -1;
                    }
                }
            }
            else
            {
                harder = -1;
            }
        }
        else
        {
            musicName = musicPath;
            levelInfo = "Unknown,Unknown";
            harder = -1;
        }
        new LevelInfo(path, musicName, harder, lv,false, levelInfo);
    }
}
